package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.ReceiveInStorageItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageReceiveOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;


//验收入库
public class InStorageReceiveOverviewActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView receiveInStorageItemList;

    private List<InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean> allData;

    private ReceiveInStorageItemListAdapter adapter;

    private String mInStorageNumber, mMaterielCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_receive_overview);
        initView(InStorageReceiveOverviewActivity.this);
        SPUtils.removeReceiveInStorageInformation(InStorageReceiveOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        mMaterielCode = "";
        initData(InStorageReceiveOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        receiveInStorageItemList = (PullableListView) activity.findViewById(R.id.receiveInStorageItemList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiveInStorageItemListRefresh);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_in_storage_receive_overview_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_in_storage_receive_list_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiveInStorageItemList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity, receiveInStorageItemList)) {
                    int index = InStorageUtils.checkReceiveBatchInfo(activity);
                    if (index == -1) {
                        Intent intent = new Intent(activity, InStorageReceiveAddOperatorActivity.class);
                        startActivity(intent);
                    } else {
                        Utils.middleToast(activity, activity.getResources().getString(R.string.activity_in_storage_receive_overview_no) + index + activity.getResources().getString(R.string.activity_in_storage_receive_overview_not_finish));
                    }
                }
            }
        });
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private void getExtra(Activity activity) {
        mInStorageNumber = activity.getIntent().getStringExtra(InStorageExtra.IN_STORAGE_NUMBER);
    }

    private void addInStorageReceiveItemListData(Context context, InStorageReceiveOverviewBean.DataBean bean, List<InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean> receiveInStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReceiveInStorageItemListAdapter(context, bean, receiveInStorageItemList, mMaterielCode, new InStorageReceiveOverviewListener() {
                @Override
                public void onNoDoubleClickListener(String materielCode) {
                    mMaterielCode = materielCode;
                    getData(context);
                }

                @Override
                public boolean onCheckData() {
                    return check(context, listView);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private boolean check(Context context, ListView listView) {
        int childCount = listView.getChildCount();
        for (int i = 1; i < childCount; i++) {
            View view = listView.getChildAt(i);
            String serialNumber = ((TextView) view.findViewById(R.id.serialNumber)).getText().toString();
            String inventoryStr = ((TextView) view.findViewById(R.id.inventory)).getText().toString();
            String inStorageNumber = ((EditText) view.findViewById(R.id.inStorageNumber)).getText().toString();
            String inStorageNumberMax = ((TextView) view.findViewById(R.id.inStorageNumberMax)).getText().toString();
            String retainDecimal = ((TextView) view.findViewById(R.id.retainDecimal)).getText().toString();
            String[] inventoryAddressArray = inventoryStr.split(" - ");
            String inventoryCode = inventoryAddressArray[0];
            String inventoryValue = inventoryAddressArray[1];
            if (TextUtils.isEmpty(inStorageNumber)) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_overview_serial) + serialNumber + context.getResources().getString(R.string.activity_in_storage_receive_overview_not_null));
                return false;
            }

            if (!RegexUtils.checkDecimal(inStorageNumber)) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_overview_serial) + serialNumber + context.getResources().getString(R.string.activity_in_storage_receive_overview_illegal));
                return false;
            }

            if (!Utils.checkRetainDecimal(inStorageNumber, retainDecimal)) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_overview_serial) + serialNumber + String.format(context.getResources().getString(R.string.common_retain_decimal_illegal), TextUtils.isEmpty(retainDecimal) ? context.getResources().getString(R.string.common_retain_decimal_unlimited) : retainDecimal));
                return false;
            }

            if (RegexUtils.checkZero(inStorageNumber)) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_overview_serial) + serialNumber + context.getResources().getString(R.string.activity_in_storage_receive_overview_greater_than_zero));
                return false;
            }
            float maxNumber = Float.parseFloat(inStorageNumberMax);
            float inStoragetNumberFloat = Float.parseFloat(inStorageNumber);
            if (inStoragetNumberFloat > maxNumber) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_overview_serial) + serialNumber + context.getResources().getString(R.string.activity_in_storage_receive_overview_restriction));
                return false;
            }
            InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean receiveInStorageItem = InStorageUtils.getReceiveInStorageItemBySerial(context, serialNumber);
            if (receiveInStorageItem != null) {
                receiveInStorageItem.setInventoryAddressCode(inventoryCode);
                receiveInStorageItem.setInventoryAddressValue(inventoryValue);
                receiveInStorageItem.setRealInStorageNumber(inStorageNumber);
                InStorageUtils.updateReceiveInStorageItem(context, receiveInStorageItem);
            }
        }
        return true;
    }

    private void getData(Context context) {
        String receiveInStorageInformation = SPUtils.getReceiveInStorageInformation(context);
        if (TextUtils.isEmpty(receiveInStorageInformation)) {
            InStorageReceiveOverviewRequest.request(context, mInStorageNumber, new GsonBeanListener<InStorageReceiveOverviewBean>() {
                @Override
                public void onSuccess(InStorageReceiveOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, InStorageReceiveOverviewBean.class);
                    SPUtils.saveReceiveInStorageInformation(context, json);
                    InStorageReceiveOverviewBean.DataBean data = bean.getData();
                    allData = bean.getData().getReceiveInStorageList();
                    addInStorageReceiveItemListData(context, data, ActivityUtils.getShowData(allData, 0), receiveInStorageItemList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            InStorageReceiveOverviewBean bean = gson.fromJson(receiveInStorageInformation, InStorageReceiveOverviewBean.class);
            if (TextUtils.isEmpty(mMaterielCode)) {
                allData = bean.getData().getReceiveInStorageList();
            } else {
                allData = InStorageUtils.getReceiveInStorageList(context, mMaterielCode);
            }
            addInStorageReceiveItemListData(context, bean.getData(), ActivityUtils.getShowData(allData, 0), receiveInStorageItemList, true);
        }
    }

    public interface InStorageReceiveOverviewListener {
        void onNoDoubleClickListener(String materielCode);

        boolean onCheckData();
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(InStorageReceiveOverviewActivity.this, InStorageReceiveOverviewActivity.this.getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                InStorageReceiveOverviewActivity.super.onBackPressed();
            }
        }).show();
    }
}
